package com.envper.mqtt.subscriber;

import com.alibaba.fastjson2.JSON;
import com.envper.common.domain.device.Payload;
import com.envper.common.service.device.DeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class EnvperMessage {
    private final DeviceService deviceService;

    @ServiceActivator(inputChannel = "mqttInputChannel")
    public void handleMessage(Message<String> message) {
        Payload payload = JSON.parseObject(message.getPayload(), Payload.class);
        deviceService.saveData(payload);
    }
}
